/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.persistance;

import java.util.List;
import java.util.ArrayList;
import java.sql.*;

/**
 *
 * @author EuiHyun
 */
public class ManagedItemListDAO extends AbstractManagedItemListDAO {

    @Override
    public ManagedItemListDTO find(String managedItemListId) throws PersistanceException {
        ManagedItemListDTO dto = null;
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select FILEPATH from APP.MANAGEDITEMLIST where FILEPATH = '"+managedItemListId+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                dto = new ManagedItemListDTO();
                
                dto.setFilePath(rs.getString("FILEPATH"));
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ManagedItemListDAO.find: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dto; 
    }

    @Override
    public List<ManagedItemListDTO> list() throws PersistanceException {
        List<ManagedItemListDTO> dtoArray = new ArrayList<ManagedItemListDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select FILEPATH from APP.MANAGEDITEMLIST";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                ManagedItemListDTO dto = new ManagedItemListDTO();
                dto.setFilePath(rs.getString("FILEPATH"));
                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ManagedItemListDAO.list: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }

    @Override
    public void insert(ManagedItemListDTO managedItemListDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        //String sql="insert into APP.MANAGEDITEMLIST (FILEPATH) "  + "values ('"+managedItemListDTO.getFilePath()+"')";
        
        String sql="insert into APP.MANAGEDITEMLIST (FILEPATH) "  + "values (?)";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, managedItemListDTO.getFilePath());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ManagedItemListDAO.insert: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }
    }

    @Override
    public void update(ManagedItemListDTO managedItemListDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        //String sql="update APP.MANAGEDITEMLIST set FILEPATH= '"+ managedItemListDTO.getFilePath() + "' where FILEPATH = '"+managedItemListDTO.getFilePath()+"'";
        
        String sql="update APP.MANAGEDITEMLIST set FILEPATH=? where FILEPATH =?";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, managedItemListDTO.getFilePath());
            stmt.setString(2, managedItemListDTO.getFilePath());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ManagedItemListDAO.update: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }     
    }

    @Override
    public void delete(String managedItemListId) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.MANAGEDITEMLIST "  
                   +"WHERE FILEPATH = '"+managedItemListId+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ManagedItemListDAO.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }
}
